import { Action } from "redux";
import { GlobalState } from "@/redux/interface";
import { Draft, produce } from "immer";
import * as types from "@/redux/mutation-types";

const globalState: GlobalState = {
  token: "",
  userInfo: "",
  assemblySize: "middle",
  language: "",
  themeConfig: {
    // 默认 primary 主题颜色
    primary: "#1890ff",
    // 深色模式
    isDark: false,
    // 色弱模式(weak) || 灰色模式(gray)
    weakOrGray: "",
    // 面包屑导航
    breadcrumb: true,
    // 标签页
    tabs: true,
    // 页脚
    footer: true,
  },
};

// 定义具体的 Action 类型，避免使用 any
interface SetUserInfoAction extends Action {
  type: typeof types.SET_USER_INFO;
  userInfo: object;
}


interface SetTokenAction extends Action {
    type: typeof types.SET_TOKEN;
    token: string;
  }

interface SetAssemblySizeAction extends Action {
  type: typeof types.SET_ASSEMBLY_SIZE;
  assemblySize: GlobalState["assemblySize"];
}

interface SetLanguageAction extends Action {
  type: typeof types.SET_LANGUAGE;
  language: GlobalState["language"];
}

interface SetThemeConfigAction extends Action {
  type: typeof types.SET_THEME_CONFIG;
  themeConfig: GlobalState["themeConfig"];
}

type ActionType =
  | SetTokenAction
  | SetAssemblySizeAction
  | SetLanguageAction
  | SetThemeConfigAction
  | SetUserInfoAction;
// global reducer
const global = (state: GlobalState = globalState, action: ActionType) => {
  return produce(state, (draftState: Draft<GlobalState>) => {
    switch (action.type) {
      case types.SET_TOKEN:
        draftState.token = action.token;
        break;
      case types.SET_ASSEMBLY_SIZE:
        draftState.assemblySize = action.assemblySize;
        break;
      case types.SET_LANGUAGE:
        draftState.language = action.language;
        break;
      case types.SET_THEME_CONFIG:
        draftState.themeConfig = action.themeConfig;
        break;
    case types.SET_USER_INFO:
        draftState.userInfo = action.userInfo;
        break;
      default:
        // draftState = state;
        break;
    }
  });
};

export default global;
